/*
 * RepairDAO.java
 *
 * Created on March 22, 2007, 12:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package finalintex2web;
import java.util.*;
import java.io.*;
import java.sql.*;

/**
 *
 * @author craig
 */
public class RepairDAO extends RevenueSourceDAO {
    
    
    /** Creates a new instance of RepairDAO */
    public RepairDAO() {
        super();
    }
    protected static RevenueSourceDAO instance = null;
        
    /**
     * 
     * @return 
     */
    public static synchronized RevenueSourceDAO getInstance(){
        if (instance == null) {
            instance = new RepairDAO();
        }
        return instance;
    }
    
    /**
     * 
     * @return 
     */
    public RevenueSourceBO create(){
        String id = GUID.generate();
        RepairBO repair = new  RepairBO(id);
        Cache c = Cache.getInstance();
        c.put(repair.getId(), repair);
        return repair;
    }
    
    /**
     * 
     * @param id 
     * @param conn 
     * @throws java.sql.SQLException 
     * @throws intex2.DataException 
     * @throws intex2.ConnectionPoolException 
     * @return 
     */
    public RevenueSourceBO read(String id, Connection conn) throws SQLException, DataException, ConnectionPoolException{
     if(Cache.getInstance().containsKey(id)){
            RepairBO repair = (RepairBO)Cache.getInstance().get(id);
            return repair;
            } 
                
           try{        
           PreparedStatement stmt = conn.prepareStatement("SELECT * FROM repair WHERE id=?");
           stmt.setString(1,id);
           ResultSet rs =  stmt.executeQuery();
           
           ConnectionPool.getInstance().release(conn);
            
            if (rs.next()) {
                RepairBO repair = new RepairBO(id);
                Cache.getInstance().put(id,repair);
                super.read(id);
                repair.setLabor(rs.getInt("labor"));
                repair.setBegindate(rs.getString("startdate"));
                repair.setCompleteddate(rs.getString("completiondate"));
                repair.setDescription(rs.getString("description"));
                repair.setDatepickedup(rs.getString("pickupdate"));
                repair.setEmp(EmployeeDAO.getInstance().read(rs.getString("employee")));
                Cache.getInstance().put(id,repair);      
                repair.setIndb(true);
                stmt.close();
                return repair;
               

            }else{
                throw new DataException("RevenueSourceBO was not found in the database.");
            }
           
            }catch (ConnectionPoolException e){
                throw new ConnectionPoolException("Could not retrieve a connection from the database", e);
            
            }catch (SQLException e){
                throw new DataException("SQL error", e);
            }
    }
    
    /**
     * 
     * @param revsrc 
     * @throws intex2.DataException 
     * @throws intex2.ConnectionPoolException 
     */
     public synchronized void save(RevenueSourceBO revsrc) throws DataException,ConnectionPoolException {
            
        try {
            Connection conn = ConnectionPool.getInstance().get();
            save(revsrc,conn);
            ConnectionPool.getInstance().release(conn);
        }catch (SQLException e) {
            //rollback
            throw new DataException("Could not retrieve record for id=" + revsrc.getId(), e);
            
            }catch (ConnectionPoolException e){
            //throw new ConnectionPoolException("Could not retrieve record for id=" + e);
                e.printStackTrace();
        }
        
        // release the connection back to the pool
        return;
    }
     synchronized void save(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException {
        // check the dirty flag in the RevenueSourceDBO.  if it is dirty, 
        // run update or insert
        if (revsrc.isDirty()) {
            if (revsrc.isIndb()) {
               update(revsrc, conn);
            }else{
               insert(revsrc, conn);
            }
            revsrc.setDirty(false);
        Cache.getInstance().touch(revsrc.getId());
     
        }
        
    }

    private synchronized void update(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException{
        PreparedStatement stmt = conn.prepareStatement("UPDATE repair SET startdate = ?, completiondate = ?, description = ?, pickupdate = ?, employee = ?, labor = ? WHERE id=? ");
        super.save(revsrc,conn);
        RepairBO repair = (RepairBO)revsrc;
        stmt.setString(1,repair.getBegindate());
        stmt.setString(2,repair.getCompleteddate());
        stmt.setString(3,repair.getDescription());
        stmt.setString(4,repair.getDatepickedup());
        stmt.setString(5,repair.getEmp().getId());
        stmt.setInt(6,repair.getLabor());
        stmt.setString(7,repair.getId());
        stmt.execute();
        stmt.close();
        ConnectionPool.getInstance().release(conn);
        
    }
    private synchronized void insert(RevenueSourceBO revsrc, Connection conn) throws SQLException, DataException, ConnectionPoolException{
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO repair (startdate,completiondate,description,pickupdate,employee,labor,id) VALUES (?,?,?,?,?,?,?)");
        
        super.save(revsrc,conn);
        RepairBO repair = (RepairBO)revsrc;
        stmt.setString(1,repair.getBegindate());
        stmt.setString(2,repair.getCompleteddate());
        stmt.setString(3,repair.getDescription());
        stmt.setString(4,repair.getDatepickedup());
        stmt.setString(5,repair.getEmp().getId());
        stmt.setInt(6,repair.getLabor());
        stmt.setString(7,repair.getId());
        stmt.execute();
        stmt.close();
        // tell the RevenueSourceDBO that it's now in the db (so we call update next time not insert)
        revsrc.setIndb(true);
        ConnectionPool.getInstance().release(conn);
        
    
}
}